package org.mobile.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.mobile.logic.PhoneLogic;

public class Phone {
	private int phoneId;
	private String model;
	private int price;
	private float hardiness;
	private float camera;
	private float height;
	private float weight;
	private float depth;
	private float width;
	private float colors;
	private String resolution;
	private float screenSize;
	private float talkTime2g;
	private float talkTime3g;
	private float standBy2g;
	private float standBy3g;
	private float buildInMem;
	private int ram;
	private float maxCardSize;
	private float nrOfApp;
	private float procClock;
	private float releaseDate;
	private float popularity;
	private int hsdpaitd;
	private int gorillaglass;
	private float design;
	private int autofocus;
	private boolean gorillaB;
	private boolean focusB;
	private Phone selectedPhone;
	private boolean HSDPA;
	private boolean HSUPA;
	private boolean HSPA;
	private boolean HSPAP;
	private boolean WIFI;
	
	public int getPhoneId() {
		return phoneId;
	}
	public void setPhoneId(int phoneId) {
		this.phoneId = phoneId;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
	public String getResolution() {
		return resolution;
	}
	public void setResolution(String resolution) {
		this.resolution = resolution;
	}
	
	public int getRam() {
		return ram;
	}
	public void setRam(int ram) {
		this.ram = ram;
	}
	
	public int getHsdpaitd() {
		return hsdpaitd;
	}
	public void setHsdpaitd(int hsdpaitd) {
		this.hsdpaitd = hsdpaitd;
	}
	public int getGorillaglass() {
		return gorillaglass;
	}
	public void setGorillaglass(int gorillaglass) {
		this.gorillaglass = gorillaglass;
	}
	
	public int getAutofocus() {
		return autofocus;
	}
	public void setAutofocus(int autofocus) {
		this.autofocus = autofocus;
	}
	public float getHardiness() {
		return hardiness;
	}
	public void setHardiness(float hardiness) {
		this.hardiness = hardiness;
	}
	public float getCamera() {
		return camera;
	}
	public void setCamera(float camera) {
		this.camera = camera;
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public float getWeight() {
		return weight;
	}
	public void setWeight(float weight) {
		this.weight = weight;
	}
	public float getDepth() {
		return depth;
	}
	public void setDepth(float depth) {
		this.depth = depth;
	}
	
	public float getColors() {
		return colors;
	}
	public void setColors(float colors) {
		this.colors = colors;
	}
	public float getScreenSize() {
		return screenSize;
	}
	public void setScreenSize(float screenSize) {
		this.screenSize = screenSize;
	}
	public float getTalkTime2g() {
		return talkTime2g;
	}
	public void setTalkTime2g(float talkTime2g) {
		this.talkTime2g = talkTime2g;
	}
	public float getTalkTime3g() {
		return talkTime3g;
	}
	public void setTalkTime3g(float talkTime3g) {
		this.talkTime3g = talkTime3g;
	}
	public float getStandBy2g() {
		return standBy2g;
	}
	public void setStandBy2g(float standBy2g) {
		this.standBy2g = standBy2g;
	}
	public float getStandBy3g() {
		return standBy3g;
	}
	public void setStandBy3g(float standBy3g) {
		this.standBy3g = standBy3g;
	}
	public float getBuildInMem() {
		return buildInMem;
	}
	public void setBuildInMem(float buildInMem) {
		this.buildInMem = buildInMem;
	}
	public float getMaxCardSize() {
		return maxCardSize;
	}
	public void setMaxCardSize(float maxCardSize) {
		this.maxCardSize = maxCardSize;
	}
	public float getNrOfApp() {
		return nrOfApp;
	}
	public void setNrOfApp(float nrOfApp) {
		this.nrOfApp = nrOfApp;
	}
	public float getProcClock() {
		return procClock;
	}
	public void setProcClock(float procClock) {
		this.procClock = procClock;
	}
	public float getReleaseDate() {
		return releaseDate;
	}
	public void setReleaseDate(float releaseDate) {
		this.releaseDate = releaseDate;
	}
	public float getPopularity() {
		return popularity;
	}
	public void setPopularity(float popularity) {
		this.popularity = popularity;
	}
	public float getDesign() {
		return design;
	}
	public void setDesign(float design) {
		this.design = design;
	}
	public float getWidth() {
		return width;
	}
	public void setWidth(float width) {
		this.width = width;
	}
	
	public Phone getSelectedPhone() {
		return selectedPhone;
	}
	public void setSelectedPhone(Phone selectedPhone) {
		this.selectedPhone = selectedPhone;
	}
	
	public boolean isHSDPA() {
		return HSDPA;
	}
	public void setHSDPA(boolean hSDPA) {
		HSDPA = hSDPA;
	}
	public boolean isHSUPA() {
		return HSUPA;
	}
	public void setHSUPA(boolean hSUPA) {
		HSUPA = hSUPA;
	}
	public boolean isHSPA() {
		return HSPA;
	}
	public void setHSPA(boolean hSPA) {
		HSPA = hSPA;
	}
	public boolean isHSPAP() {
		return HSPAP;
	}
	public void setHSPAP(boolean hSPAP) {
		HSPAP = hSPAP;
	}
	public boolean isWIFI() {
		return WIFI;
	}
	public void setWIFI(boolean wIFI) {
		WIFI = wIFI;
	}
	
	public String getHsdpaitdAsString(){
		String hsdpaString= Integer.toString(hsdpaitd);
		for(int i=5-hsdpaString.length() ; i>0; i--) 
			hsdpaString= "0"+ hsdpaString;
		return hsdpaString;
	}
	
	public String getPhones(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		List phones= (List) sessionMap.get("phones");
		if(phones == null){
			phones= PhoneLogic.getInstance().getAllPhones();
			sessionMap.put("phones", phones);
			System.out.println(phones);
		}
		return "phones.jsp?faces-redirect=true";
		
	}
	
	public String addPhone(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		List phones= (List) sessionMap.get("phones");
		String HSDPAITD = "";
		
		if(isHSDPA()) {
			HSDPAITD += "1";
		}
		else {
			HSDPAITD += "0";
		}
		if(isHSUPA()) {
			HSDPAITD += "1";
		}
		else {
			HSDPAITD += "0";
		}
		if(isHSPA()) {
			HSDPAITD += "1";
		}
		else {
			HSDPAITD += "0";
		}
		if(isHSPAP()) {
			HSDPAITD += "1";
		}
		else {
			HSDPAITD += "0";
		}
		if(isWIFI()) {
			HSDPAITD += "1";
		}
		else {
			HSDPAITD += "0";
		}
		
		hsdpaitd = Integer.parseInt(HSDPAITD);
		
		boolean result= PhoneLogic.getInstance().addPhone(this, HSDPAITD);
		if(result){
			phones.add(this);
			sessionMap.put("phones", phones);
			return "success";	
		}
		else {
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Dodawianie telefonu nie powiodło się");
			return "failure";		
		}
	}
	
	public String  removePhone(){
		boolean result= PhoneLogic.getInstance().removePhone(selectedPhone.phoneId);
		if(result){
			List phones= PhoneLogic.getInstance().getAllPhones();
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("phones", phones);
			return "success";
		}
		else {
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("error", "Usuwanie telefonu nie powiodło się");
			return "failure";		
		}
	}
	
	public String addToCart(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		List cart= (List) sessionMap.get("cart");
		if(cart == null){
			cart= new ArrayList<Phone>();
		}
		cart.add(selectedPhone);
		sessionMap.put("cart", cart);
		return "success";
		
	}
	
	public String removeFromCart(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		int index= Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("index"));
		List cart= (List) sessionMap.get("cart");
		cart.remove(index);
		sessionMap.put("cart", cart);
		return null;
	}
	
	public boolean getGorillaB(){
		if(gorillaglass == 1)
			return true;
		else
			return false;
	}
	
	public void setGorillaB(boolean gorrila){
		if(gorrila)
			gorillaglass= 1;
		else
			gorillaglass= 0;
	}
	
	public boolean getFocusB(){
		if(autofocus == 1)
			return true;
		else
			return false;
	}
	
	public void setFocusB(boolean focus){
		if(focus)
			autofocus= 1;
		else
			autofocus= 0;
	}
	
	
}
